<template>
  <div class="app-container">
    <div class="header">
      <div class="word">
        <div class="w1">
          用户管理系统
        </div>
        <div class="w2">
          在用户管理系统中你可以查看各个用户基本信息，同时还可以实现修改用户信息，添加用户等功能。
        </div>
      </div>
      <img src="../../static/u=2179590687,2836296523&fm=253&fmt=auto&app=138&f=JPEG.webp" class="imgDri">
    </div>
    <div class="index">
      <div class="searchHead">
        <el-button type="primary" @click="outerVisible = true">新增用户信息</el-button>
        <el-input v-model="input3" placeholder="请输入要查询的用户的id" class="searchIuput">
          <!-- <el-select slot="prepend" v-model="select" class="sel3" placeholder="请选择" style="width: 120px;">
            <el-option label="用户名" value="productName" />
            <el-option label="密码" value="employeeName" />
            <el-option label="真实姓名" value="isOrganic" />
            <el-option label="电话号码" value="companyName" />
            <el-option label="是否转基因" value="isTransgenic" />
          </el-select> -->
          <el-button slot="append" v-model="searchInf" icon="el-icon-search" @click="searchMes" />
        </el-input>
      </div>
      <el-dialog title="新增种子种苗选型表" :visible.sync="outerVisible">
        <el-form class="form">
          <el-form-item class="item" label="用户名" :label-width="formLabelWidth">
            <el-input v-model="addMes.username" class="in" autocomplete="off" />
          </el-form-item>
          <el-form-item class="item" label="密码" :label-width="formLabelWidth">
            <el-input v-model="addMes.password" class="in" autocomplete="off" />
          </el-form-item>
          <el-form-item class="item" label="真实姓名" :label-width="formLabelWidth">
            <el-input v-model="addMes.realName" class="in" autocomplete="off" />
          </el-form-item>
          <el-form-item class="item" label="电话号码" :label-width="formLabelWidth">
            <el-input v-model="addMes.phone" class="in" autocomplete="off" />
          </el-form-item>
          <el-form-item class="item" label="头像图片地址" :label-width="formLabelWidth">
            <el-input v-model="addMes.avater" class="in" autocomplete="off" />
          </el-form-item>
          <div style="margin-left:52px;margin-bottom:24px">
            <label style="height: 36px;line-height: 36px;padding: 0 12px 0 0;">用户类型</label>
            <el-select v-model="addMes.rank" class="sel2" placeholder="请选择" style="width: 186px;">
              <el-option label="普通用户" :value="0" />
              <el-option label="vip" :value="1" />
              <el-option label="管理员" :value="2" />
            </el-select>
          </div>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="outerVisible = false">取 消</el-button>
          <el-button type="primary" @click="addDri()">确 定</el-button>
        </div>
      </el-dialog>

      <el-table :data="mes" type="" style="width: 100%">
        <el-table-column label="头像">
          <template scope="scope">
            <el-image style="width: 100px; height: 100px" :src="scope.row.avater" />
          </template>
        </el-table-column>
        <el-table-column prop="userId" label="用户id" width="180" />
        <el-table-column prop="username" label="用户名" />
        <el-table-column prop="password" label="密码" />
        <el-table-column prop="realName" label="真实姓名" />
        <el-table-column prop="phone" label="电话号码" />
        <el-table-column prop="avater" label="头像图片地址" width="180" />

        <el-table-column prop="rank" label="用户类型">
          <template scope="scope">
            <!-- {{ scope.row.rank==0?'普通用户':'' }}
            {{ scope.row.rank==1?'vip':'' }}
            {{ scope.row.rank==2?'管理员':'' }} -->
            <el-select
              v-model="scope.row.rank"
              class="sel2"
              placeholder="请选择"
              style="width: 102px;"
              :disabled="scope.row.rank==0?false:true"
              @blur="updateRank(scope.row)"
            >
              <el-option label="普通用户" :value="0" />
              <el-option label="vip" :value="1" />
              <el-option label="管理员" :value="2" disabled />
            </el-select>
          </template>
        </el-table-column>
        <!-- </el-table-column> -->
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="text" @click="changeMsg(mes[scope.$index],scope.$index)">修改</el-button>
            <el-button type="text" style="color:red" @click="mes[scope.$index].isShowDel = true">删除</el-button>

            <el-dialog :modal-append-to-body="false" title="提示" :visible.sync="mes[scope.$index].isShowDel" width="30%">
              <span>确认要删除此条信息吗？</span>
              <span slot="footer" class="dialog-footer">
                <el-button @click="isShowDel = false">取 消</el-button>
                <el-button type="primary" @click="driveDel(scope.$index)">确 定</el-button>
              </span>
            </el-dialog>

            <el-dialog
              :modal-append-to-body="false"
              title="修改信息"
              :visible.sync="mes[scope.$index].isShow"
              :before-close="handleClose"
            >
              <el-form class="form">
                <el-form-item class="item" label="用户id" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].userId" autocomplete="off" disabled />
                </el-form-item>
                <el-form-item class="item" label="用户名" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].username" autocomplete="off" disabled />
                </el-form-item>
                <el-form-item class="item" label="密码" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].password" autocomplete="off" />
                </el-form-item>
                <el-form-item class="item" label="真实姓名" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].realName" autocomplete="off" />
                </el-form-item>
                <el-form-item class="item" label="电话号码" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].phone" class="in" autocomplete="off" />
                </el-form-item>
                <el-form-item class="item" label="头像图片地址" :label-width="formLabelWidth">
                  <el-input
                    v-model="mes[scope.$index].avater"
                    autocomplete="off"
                    type="textarea"
                    style="width: 350px;"
                  />
                </el-form-item>
                <div style="margin-left:10px;height: 61px;">
                  <label
                    style="display:inline-block;height: 36px;line-height: 36px;padding: 0 12px 0 0;width: 110px;text-align: right;"
                  >用户类型</label>
                  <el-select
                    v-model="mes[scope.$index].rank"
                    class="sel2"
                    placeholder="请选择"
                    style="width: 186px;"
                    disabled
                  >
                    <el-option label="普通用户" :value="0" />
                    <el-option label="vip" :value="1" />
                    <el-option label="管理员" :value="2" />
                  </el-select>
                </div>
              </el-form>

              <div slot="footer" class="dialog-footer">
                <el-button @click="cancelChangeMsg(scope.$index)">取 消</el-button>
                <el-button type="primary" @click="changeInspectionForm(scope.$index,mes[scope.$index])">确 定</el-button>
              </div>

            </el-dialog>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          :page-size="5"
          layout="prev, pager, next, jumper"
          :total="pageSize"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import request from '../../../utils/request'
export default {
  name: 'FarmlandSelection',
  data() {
    return {
      mes: [],
      firstMes: [],
      allMes: [],
      index: null,
      dialogTableVisible: false,
      formLabelWidth: '120px',
      addMes: {},
      dialogFormVisible: false,
      outerVisible: false,
      pageSize: null,
      pageNum: 1,
      val: 1,
      deliverPageNum: 1,
      deliverPageSize: null,
      select: '',
      searchInf: '',
      input3: '',
      total: null,
      allEmployee: [],
      allProduct: [],
      allCompany: [],
      ShowDel: false
    }
  },
  async mounted() {
    this.getDrive()
  },
  methods: {
    async getDrive() {
      var a = JSON.stringify({})
      console.log(a)
      await request({
        method: 'get',
        url: 'mall/user/pageUser/' + this.val + '/5'
      }).then((response) => {
        console.log(response, 'getAll')
        this.mes = response.data.records
        // console.log(response, 'this.mes')
        for (let i = 0; i < this.mes.length; i++) {
          this.$set(this.mes[i], 'isShow', false)
          this.$set(this.mes[i], 'isShowDel', false)
        }
        this.pageSize = response.data.total
        this.total = response.data.total
      })
    },
    changeMsg(mesIndex, index) {
      console.log(index, 'index')
      mesIndex.isShow = true
      this.firstMes = JSON.parse(JSON.stringify(this.mes))[index]
      this.index = index
      console.log(this.mes[index], 'mesIndex')
      // console.log('this.firstMes', this.firstMes);
    },

    async changeInspectionForm(index, mes) {
      var changMes = JSON.parse(JSON.stringify(this.mes))[index]
      console.log(mes, index, 'changMes000')
      const newChangMes = {
        userId: changMes.userId,
        username: changMes.username,
        password: changMes.password,
        realName: changMes.realName,
        phone: changMes.phone,
        rank: changMes.rank,
        avater: changMes.avater
      }
      // console.log(newChangMes, "newChangMes");
      await request({
        method: 'put',
        url: '/mall/user/updatewithId',
        data: newChangMes
      }).then(response => {
        // console.log(response, '@@', newChangMes)
        this.getDrive()
        if (response.statusCode == 20000) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '发生错误，修改失败',
            type: 'warning'
          })
        }
        this.mes[index].isShow = false
      })
    },
    cancelChangeMsg(index) {
      this.mes[index].isShow = false
      // this.isShowDel = false;
      // console.log('111', index);
      this.mes[index] = JSON.parse(JSON.stringify(this.firstMes))
      this.getDrive()
    },
    async driveDel(index) {
      // console.log(this.mes[index].userId);
      await request({
        method: 'delete',
        url: '/mall/user/deleteById/' + this.mes[index].userId
      }).then(response => {
        // console.log(response)
        this.val = 1
        this.getDrive()
        if (response.statusCode == 20000) {
          this.dialogFormVisible = false
          this.$message({
            message: '删除成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '发生错误，删除失败',
            type: 'warning'
          })
        }
      })
      this.mes[index].isShowDel = false
      // this.isShowDel = false;
    },

    async addDri() {
      // console.log('this.addMes', this.addMes)
      if (this.addMes.username && this.addMes.password && this.addMes.rank != null) {
        request({
          method: 'post',
          url: '/mall/user/register',
          data: this.addMes
        }).then(response => {
          // console.log(response)
          if (response.statusCode == 20000) {
            this.addMes = {}
            this.getDrive()
            this.$message({
              message: '添加成功',
              type: 'success'
            })
            this.outerVisible = false
          } else {
            this.$message({
              message: '发生错误，添加失败',
              type: 'warning'
            })
          }
        })
      } else {
        this.$message({
          message: '用户名、密码、用户类型均不可为空',
          type: 'warning'
        })
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.val = val
      request({
        method: 'get',
        url: '/mall/user/pageUser/' + val + '/5'
      }).then((response) => {
        // console.log(response.data, '/mall/user/pageUser/')
        this.mes = response.data.records
        for (let i = 0; i < this.mes.length; i++) {
          this.$set(this.mes[i], 'isShow', false)
          this.$set(this.mes[i], 'isShowDel', false)
        }
      })
    },

    searchMes() {
      // console.log(this.input3, 'input3');
      if (this.input3) {
        request({
          method: 'get',
          url: '/mall/user/getById/' + this.input3
        }).then((response) => {
          console.log(response.data, 'searchMes')
          if (response.statusCode == 20000) {
            this.$message({
              message: '查询成功',
              type: 'success'
            })
            this.mes = []
            this.mes[0] = response.data

            //   console.log('response', response)
          } else {
            this.$message({
              message: '查无该用户，查询失败',
              type: 'warning'
            })
          }
        })
      } else {
        this.getDrive()
      }
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
          this.addMes = {}
          this.cancelChangeMsg(this.index)
        })
        .catch(_ => { })
    },
    async updateRank(mes) {
      console.log(mes, 'mes')
      if (mes.rank != 0) {
        await request({
          method: 'put',
          url: '/mall/user/updateRankById',
          data: mes
        }).then((response) => {
          console.log(response, 'updateRank')
          if (response.statusCode == 20000) {
            this.$message({
              message: '更改等级成功',
              type: 'success'
            })
            // this.mes = response.data
            //   console.log('response', response)
          } else {
            this.$message({
              message: '更改等级失败',
              type: 'warning'
            })
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.header {
  width: 100%;
  height: 350px;
  // border: 1px solid black;
  display: flex;
  margin-bottom: 20px;
  justify-content: space-between;
}

.form {
  display: flex;
  flex-wrap: wrap;
  // border: 1px solid black;
  // justify-content: space-around;
}

.sel {
  width: 360px;
  display: flex;
  height: 50px;
  // border: 1px solid black;
  // border: 1px solid black;
  justify-content: flex-end;
  align-content: center;
  // border: 1px solid black;
}

.sel2 {
  width: 240px;
}

.page {
  width: 100%;
  display: flex;
  justify-content: right;
}

.imgDri {
  height: 300px;
  width: 400px;
}

.w1 {
  height: 80px;
  font-size: 25px;
  font-weight: 350;
}

.w2 {
  height: 50px;
  font-size: 15px;
  font-weight: 250;
}

.searchHead {
  display: flex;
  justify-content: space-between;
}

.searchIuput {
  width: 350px;
}

.sel3 {
  width: 120px;
}
</style>

